package com.education.wisdom.controller.app;


import com.alibaba.fastjson.JSONObject;
import com.education.common.core.BaseController;
import com.education.common.pojo.SysUserOther;
import com.education.common.utils.LogUtil;
import com.education.common.utils.UUIDUtils;
import com.education.wisdom.feign.UserFeign;
import com.education.wisdom.pojo.CurrencyRecord;
import com.education.wisdom.pojo.Help;
import com.education.wisdom.pojo.HelpResources;
import com.education.wisdom.pojo.UserAccount;
import com.education.wisdom.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 求助 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-01-24
 */
@Controller
@Api(tags = {"求助 信息操作处理"})
@CrossOrigin
@Slf4j
@RequestMapping("/appHelp")
public class AppHelpController extends BaseController
{
    private String prefix = "system/help";
	
	@Autowired
	private IHelpService helpService;
	@Autowired
	private IUserAccountService userAccountService;
	@Autowired
	private ICurrencyRecordService currencyRecordService;
	@Autowired
    private ISysAppLoginService SysAppLoginService;
	@Autowired
	private IHelpResourcesService helpResourcesService;
	@Autowired
	private UserFeign userFeign;
	
	@GetMapping()
	public String help()
	{
	    return prefix + "/help";
	}
	
	/**
	 * 查询求助列表
	 */
	@ApiOperation("查询求助列表")
	@GetMapping("/list")
	@ResponseBody
	public void list(HttpServletResponse response, HttpServletRequest request) throws Exception{
		log.info(LogUtil.LogInfo("查询求助列表",new Date()));
	        List<Help> list = helpService.selectList();
			writeJSON(list, response, "200", "成功");
	}

	/**
	 * 根据用户id查询求助列表
	 */
	@ApiOperation("条件查询求助列表")
	@GetMapping("/lists")
	@ResponseBody
	public void listByuserId(HttpServletResponse response,Help help,Integer pageNo) throws Exception{
		Integer pageSize = 20;
		log.info(LogUtil.LogInfo("条件查询求助列表",new Date()));
		PageHelper.startPage(pageNo.intValue(), pageSize.intValue());
	        List<Help> list = helpService.selectHelpList(help);
			writeJSON(new PageInfo<Help>(list), response, "200", "成功");
	}
	
	/**
	 * 查询所有求助列表
	 */
	@ApiOperation("查询我发布的所有求助列表")
	@GetMapping("/listAllForMe")
	@ResponseBody
	public void listAllForMe(HttpServletResponse response,HttpServletRequest request,String userId,Integer pageNo) throws Exception{
			Integer pageSize = 20;
			log.info(LogUtil.LogInfo("查询我发布的所有求助列表",new Date()));
			PageHelper.startPage(pageNo.intValue(), pageSize.intValue());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", userId);
	        List<Help> list = helpService.selectAllList(map);
			writeJSON(new PageInfo<Help>(list), response, "200", "成功");
	}
	
	/**
	 * 查询所有求助列表
	 */
	@ApiOperation("查询所有求助列表")
	@GetMapping("/listAll")
	@ResponseBody
	public void listAll(HttpServletResponse response,HttpServletRequest request,String userId,Integer pageNo) throws Exception{
			Integer pageSize = 20;
			log.info(LogUtil.LogInfo("查询所有求助列表",new Date()));
			PageHelper.startPage(pageNo.intValue(), pageSize.intValue());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", userId);
	        List<Help> list = helpService.selectListAll(map);
			writeJSON(new PageInfo<Help>(list), response, "200", "成功");
	}
	
	/**
	 * 根据时间查询求助列表
	 */
	@ApiOperation("根据时间查询求助列表")
	@GetMapping("/listByTime")
	@ResponseBody
	public void listByTime(HttpServletResponse response,HttpServletRequest request,Integer pageNo,Integer pageSize) throws Exception{
		log.info(LogUtil.LogInfo("根据时间查询求助列表",new Date()));
			PageHelper.startPage(pageNo.intValue(), pageSize.intValue());
	        List<Help> list = helpService.selectListByTime();
			writeJSON(new PageInfo<Help>(list), response, "200", "成功");
	} 
	
	/**
	 * 根据达人币查询我的求助列表
	 */
	@ApiOperation("根据达人币查询我的求助列表")
	@GetMapping("/listMyByCurrency")
	@ResponseBody
	public void listMyByCurrency(HttpServletResponse response,HttpServletRequest request,String userId,Integer pageNo) throws Exception{
			Integer pageSize = 20;
			log.info(LogUtil.LogInfo("根据达人币查询我的求助列表",new Date()));
		 	PageHelper.startPage(pageNo.intValue(), pageSize.intValue());
		 	Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", userId);
	        List<Help> list = helpService.selectByCurrencys(map);
			writeJSON(new PageInfo<Help>(list), response, "200", "成功");
	} 
	
	/**
	 * 根据浏览数查询我的求助列表
	 */
	@ApiOperation("根据浏览数查询我的求助列表")
	@GetMapping("/listMyByBrowse")
	@ResponseBody
	public void listMyByBrowse(HttpServletResponse response,HttpServletRequest request,String userId,Integer pageNo) throws Exception{
			Integer pageSize = 20;
			log.info(LogUtil.LogInfo("根据浏览数查询求助列表",new Date()));
			PageHelper.startPage(pageNo.intValue(), pageSize.intValue());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", userId);
	        List<Help> list = helpService.selectListByBrowses(map);
			writeJSON(new PageInfo<Help>(list), response, "200", "成功");
	} 
	
	/**
	 * 根据达人币查询求助列表
	 */
	@ApiOperation("根据达人币查询求助列表")
	@GetMapping("/listByCurrency")
	@ResponseBody
	public void listByCurrency(HttpServletResponse response,HttpServletRequest request,String userId,Integer pageNo) throws Exception{
			Integer pageSize = 20;
			log.info(LogUtil.LogInfo("根据达人币查询求助列表",new Date()));
		 	PageHelper.startPage(pageNo.intValue(), pageSize.intValue());
		 	Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", userId);
	        List<Help> list = helpService.selectByCurrency(map);
			writeJSON(new PageInfo<Help>(list), response, "200", "成功");
	} 
	
	/**
	 * 根据浏览数查询求助列表
	 */
	@ApiOperation("根据浏览数查询求助列表")
	@GetMapping("/listByBrowse")
	@ResponseBody
	public void listByBrowse(HttpServletResponse response,HttpServletRequest request,String userId,Integer pageNo) throws Exception{
			Integer pageSize = 20;
			log.info(LogUtil.LogInfo("根据浏览数查询求助列表",new Date()));
			PageHelper.startPage(pageNo.intValue(), pageSize.intValue());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", userId);
	        List<Help> list = helpService.selectListByBrowse(map);
			writeJSON(new PageInfo<Help>(list), response, "200", "成功");
	} 
	/**
	 * 查询选中求助信息
	 */
	@ApiOperation("查询选中求助信息")
	@GetMapping("/selectOne")
	@ResponseBody
	public void selectOne(HttpServletResponse response,HttpServletRequest request,Integer id,String userId,Integer pageNo,Integer pageSize) throws Exception{
		log.info(LogUtil.LogInfo("查询选中求助信息",new Date()));
	        Help help = helpService.selectHelpById(id);
	        if (!help.getUserId().equals(userId)) {
	        	writeJSON(help, response, "200", "成功");
			}else {
				PageHelper.startPage(pageNo.intValue(), pageSize.intValue());
				List<HelpResources> list = helpResourcesService.selectHelpResourcesByHelpId(id);
				PageInfo<HelpResources> page = new PageInfo<HelpResources>(list);
				JSONObject dataJson=new JSONObject();
			    dataJson.put("page", page);
				writeJSON(help, response, "201", dataJson.toString());
			}
			
	}
	
	/**
	 * 查询别人提供的解决方案
	 */
	@ApiOperation("查询别人提供的解决方案")
	@GetMapping("/selectList")
	@ResponseBody
	public void selectList(HttpServletResponse response,HttpServletRequest request,Integer id,Integer pageNo) throws Exception{
		
				Integer pageSize = 10;
				log.info(LogUtil.LogInfo("查询别人提供的解决方案",new Date()));
				PageHelper.startPage(pageNo.intValue(), pageSize.intValue());
				List<HelpResources> list = helpResourcesService.selectHelpResourcesByHelpId(id);
				writeJSON(new PageInfo<HelpResources>(list), response, "200", "success");
			
			
	}


	/**
	 * 查询选中求助信息
	 */
	@ApiOperation("判断是否是自己发布")
	@GetMapping("/selectTwo")
	@ResponseBody
	public void selectTwo(HttpServletResponse response,HttpServletRequest request,Integer id,String userId) throws Exception{
		log.info(LogUtil.LogInfo("判断是否是自己发布",new Date()));
	        Help help = helpService.selectHelpById(id);
	        if (!help.getUserId().equals(userId)) {
	        	writeJSON("不是自己发布", response, "200", "成功");
			}else {
				writeJSON("是自己发布", response, "201", "成功");
			}
			
	}
	
	/**
	 * 采纳稿件
	 */
	@ApiOperation("采纳稿件")
	@GetMapping("/adopt")
	@ResponseBody
	public void adopt(HttpServletResponse response,HttpServletRequest request,Integer id) throws Exception{
		log.info(LogUtil.LogInfo("查询选中求助信息",new Date()));
		HelpResources helpResources = helpResourcesService.selectHelpResourcesById(id);
		helpResources.setIsAdopt("1");
		Help help = helpService.selectHelpById(helpResources.getHelpId());
		help.setType("1");
		helpService.updateHelp(help);
		helpResourcesService.updateHelpResources(helpResources);
		//加钱
		UserAccount user = userAccountService.selectUserCurrency(helpResources.getUserId());
		user.setCurrency(user.getCurrency() + help.getCurrency());
		user.setUserId(helpResources.getUserId());
		userAccountService.updateUserAccount(user);
		//新增币流水
		CurrencyRecord currencyRecord = new CurrencyRecord();
		currencyRecord.setCurrencyId(UUIDUtils.getByUUId());
		currencyRecord.setCreateTime(new Date());
		currencyRecord.setUserId(helpResources.getUserId());
		currencyRecord.setStatus("1");
		currencyRecord.setTrxType("7");
		currencyRecord.setCurrency(new BigDecimal(help.getCurrency()));
		SysUserOther sysUserOther = userFeign.selectOnePCF(helpResources.getUserId());
		if(sysUserOther != null ){
			String relname = sysUserOther.getSysRelname();
			currencyRecord.setPayerName(relname);
		}
//		SysAppLogin sysAppLogin = SysAppLoginService.selectSysAppLoginById(helpResources.getUserId());
		currencyRecordService.insertCurrencyRecord(currencyRecord);
		writeJSON("已采纳", response, "200", "成功");
	        
	}

	/**
	 * 新增求助信息
	 */
	@ApiOperation("新增求助信息")
	@PostMapping("/addHelp")
	@ResponseBody
	public void addHelp(HttpServletResponse response,HttpServletRequest request,Help help) throws Exception{
		log.info(LogUtil.LogInfo("新增求助信息",new Date()));
		 	UserAccount user = userAccountService.selectUserCurrency(help.getUserId());
			if(user==null){
				JSONObject  dataJson=new JSONObject();
				dataJson.put("currency", help.getCurrency());
				writeJSON(0, response, "202" ,dataJson.toString());
			}else if (user.getCurrency() - help.getCurrency() >= 0) {
				help.setHelpId(UUIDUtils.getByUUId());
				help.setCreateTime(new Date());
				//扣钱
				user.setCurrency(user.getCurrency() - help.getCurrency());
				user.setUserId(help.getUserId());
				userAccountService.updateUserAccount(user);
				//新增币流水
				CurrencyRecord currencyRecord = new CurrencyRecord();
				currencyRecord.setCurrencyId(UUIDUtils.getByUUId());
				currencyRecord.setCreateTime(new Date());
				currencyRecord.setUserId(help.getUserId());
				currencyRecord.setStatus("1");
				currencyRecord.setTrxType("6");
				currencyRecord.setCurrency(new BigDecimal(help.getCurrency()));
				SysUserOther sysUserOther = userFeign.selectOnePCF(help.getUserId());
				if(sysUserOther != null ){
					String relname = sysUserOther.getSysRelname();
					currencyRecord.setPayerName(relname);
				}
//				SysAppLogin sysAppLogin = SysAppLoginService.selectSysAppLoginById(help.getUserId());
//				String relname = sysAppLogin.getUserRelname();
//				currencyRecord.setPayerName(relname);
				currencyRecordService.insertCurrencyRecord(currencyRecord);
				writeJSON(helpService.insertHelp(help), response, "200", "成功");
			}else {
//			Integer currency = help.getCurrency() - user.getCurrency();
//			JSONObject  dataJson=new JSONObject();
//			dataJson.put("currency", currency);
//			writeJSON(user.getCurrency(), response, "201", dataJson.toString());
				writeJSON("2", response, "200", "成功");
			}
	}

}
